/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.test.library;

import java.util.ArrayList;
import java.util.List;

import de.fuberlin.replication.library.Metadata;
import de.fuberlin.replication.library.Subset;
import de.fuberlin.replication.storage.MetadataDAO;

public class MetadataDAODummy implements MetadataDAO {

	List<MetadataDummy> metadatas = new ArrayList<MetadataDummy>();
	
	@Override
	public Iterable<Metadata> getAll() {
		List<Metadata> metadataList = new ArrayList<Metadata>();
		
		for(MetadataDummy m : metadatas)
			metadataList.add(new MetadataDummy(m.getName(), m.getVersion(), m.getTimestamp()));
		
		return metadataList;
	}

	@Override
	public Iterable<Metadata> getAll(Subset subset) {
		List<Metadata> metadataList = new ArrayList<Metadata>();
		
		for(MetadataDummy m : metadatas)
			if(m.subset == subset.getId())
				metadataList.add(new MetadataDummy(m.getName(), m.getVersion(), m.getTimestamp()));
		
		return metadataList;
	}

	@Override
	public Metadata get(String name) {
		for(MetadataDummy m : metadatas)
			if (m.getName().equals(name))
				return new Metadata(m.getName(), m.getVersion(), m.getTimestamp());
		
		return null;
	}

	@Override
	public void save(Subset subset, Metadata metadata) {
		boolean found = false;
		
		for(MetadataDummy m:metadatas)
			if(m.getName().equals(metadata.getName())) {
				m.setVersion(metadata.getVersion());
				m.setTimestamp(metadata.getTimestamp());
				m.subset = subset.getId();
				found = true;
			}
		
		if(!found)
			metadatas.add(new MetadataDummy(metadata.getName(), metadata.getVersion(), metadata.getTimestamp(), subset.getId()));
	}

	@Override
	public void delete(Metadata metadata) {
		Metadata obsoleteMetadata = null;
		
		for(MetadataDummy m:metadatas)
			if(m.getName().equals(metadata.getName()))
				obsoleteMetadata = m;
	
		if(obsoleteMetadata != null)
			metadatas.remove(obsoleteMetadata);
	}

	@Override
	public long getLatestTimestamp() {
		return 0;
	}
}